package com.realife.eson.common.util;

import cn.hutool.core.lang.UUID;
import cn.hutool.crypto.digest.MD5;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @version V1.0
 * @author: guangcheng
 * @date: 2022/3/13 15:48
 * @description:
 */
@Slf4j
public class UniqueCodeUtil {
    private static IdWorker idWorker = new IdWorker(1, 2);

    private UniqueCodeUtil() {

    }

    /**
     * 业务唯一标识Id码
     * 利用哈希摘要获取稳定的标识
     *
     * @param name
     * @return
     */
    public static String uniqueCode(String name) {
        String nameMD5 = MD5.create().digestHex16(name);
        return nameMD5;
    }


    /**
     * @param args 多个字符串
     * @return
     */
    public static String strUniqueCode(String... args) {
        StringBuilder stringBuilder = new StringBuilder();
        for (String str : args) {
            stringBuilder.append(str);
        }
        String res = stringBuilder.toString();
        String code = uniqueCode(res);
        log.info("[**===================> res={}, code={} **]", res, code);
        return code;
    }


    /**
     * 唯一id: 带前缀
     *
     * @return
     */
    public static String nextId(String prefix) {
        String code = String.valueOf(idWorker.nextId());
        return prefix + code;
    }


    /**
     * 唯一id
     *
     * @return
     */
    public static String nextId() {
        return String.valueOf(idWorker.nextId());
    }


    public static String token() {
        long time = System.currentTimeMillis();
        String uuidStr = UUID.randomUUID().toString().replaceAll("-", "");
        String nextId = nextId();
        return time + uuidStr + nextId;
    }


    /**
     * ==============================================================================================
     * 获取字符串MD5摘要
     *
     * @param message
     * @return
     */
    public static String getMD5(String message) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] b = md5.digest(message.getBytes("utf-8"));
            return byteToHexStringSingle(b);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }


    private static char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    private static String byteToHexStringSingle(byte[] bytes) {
        StringBuffer md5sb = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            if (Integer.toHexString(0xFF & bytes[i]).length() == 1) {
                md5sb.append("0").append(Integer.toHexString(0xFF & bytes[i]));
            } else {
                md5sb.append(Integer.toHexString(0xFF & bytes[i]));
            }
        }
        return md5sb.toString();
    }


    private static String byteToHexString(byte[] tmp) {
        String s = "";
        char[] str = new char[16 * 2];
        int k = 0;
        for (int i = 0; i < 16; i++) {
            byte byte0 = tmp[i];
            str[k++] = hexDigits[byte0 >>> 4 & 0xf];
            str[k++] = hexDigits[byte0 & 0xf];
        }
        s = new String(str);
        return s;
    }


}

